﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Genetic_algorithm.AHP
{
    class AHPMain
    {
        public void AHP()
        {
            string jz = "10,12,21,21,14,23,15,14,22";
            string[] p;
            p = jz.Split(',');
            string[] P = GetValue(p);
            int weishu = ws(P);
            double[,] array = splice(P, weishu);//构建比较矩阵
                                                //按列向量归一化
            double[,] Array1 = new double[weishu, weishu];
            for (int i = 0; i < weishu; i++)
            {
                double sumb = 0;
                //求列向量的和
                for (int j = 0; j < weishu; j++)
                {
                    sumb += array[j, i];
                }
                for (int j = 0; j < weishu; j++)
                {
                    Array1[j, i] = array[j, i] / sumb;
                }
            }
            //对Array1的行进行求和
            double[,] Array2 = new double[weishu, 1];
            for (int i = 0; i < weishu; i++)
            {
                for (int j = 0; j < weishu; j++)
                {
                    Array2[i, 0] += Array1[i, j];
                }
            }
            //对矩阵Array2进行归一化
            double sum1 = 0;
            for (int i = 0; i < weishu; i++)
            {
                sum1 += Array2[i, 0];
            }
            for (int i = 0; i < weishu; i++)
            {
                Array2[i, 0] = Array2[i, 0] / sum1;
            }
            //进行一致性校验
            double suma = 0;
            double lamda = 0;
            double CI = 0;
            double[] sum = new double[weishu];
            for (int i = 0; i < weishu; i++)
            {
                for (int j = 0; j < weishu; j++)
                {
                    sum[i] += array[i, j] * Array2[j, 0];
                }
            }
            for (int i = 0; i < weishu; i++)
            {
                suma += sum[i] / Array2[i, 0];
            }
            lamda = suma / weishu;
            CI = (lamda - weishu) / (weishu - 1);
            double[] RI = { 0, 0, 0.58, 0.9, 1.12, 1.24, 1.31, 1.41, 1.45 };
            //bool Result = false;
            double CR;
            CR = CI / RI[weishu - 1];
            if (CR < 0.1)
            {
                /*Result = true;*///通过一致性校验
                Console.WriteLine("一致性校验通过。");
            }
            else
            {
                Console.WriteLine("一致性校验未通过。");
            }
        }

        //将字符串里面的分数转换成double型
        static string[] GetValue(string[] str)
        {
            string[] fenshu = new string[str.Length];
            for (int i = 0; i < str.Length; i++)
            {
                // 判断该字符串是否是分数
                if (str[i].IndexOf("/") > 0)
                {
                    string fenzi = str[i].Substring(0, str[i].IndexOf('/'));
                    string fenmu = str[i].Split('/')[1];
                    fenshu[i] = Convert.ToDouble(Convert.ToDouble(fenzi) / Convert.ToDouble(fenmu)).ToString("f4");
                }
                else
                {
                    fenshu[i] = Convert.ToDouble(str[i]).ToString("f4");
                }
            }
            return fenshu;

        }
        static int ws(string[] sz)
        {
            int szws;
            //szws = sz.Length;
            szws = Convert.ToInt32(Math.Sqrt(sz.Length));
            return szws;
        }

        //将所输出的String格式数据转换成double型矩阵
        static double[,] splice(string[] str, int mywsl)
        {
            int mylength= str.Length;//获取整个一维数组的长度
            double[] newArr = new double[mylength];

            //字符串像double数组的复制
            for (int i = 0; i < mylength; i++)
            {
                newArr[i] = Convert.ToDouble(str[i]);
            }

            double[,] arr = new double[mywsl, mywsl];
            int m = 0;
            for (int i = 0; i < mywsl; i++)
            {
                for (int j = 0; j < mywsl; j++)
                {
                    arr[i, j] = Convert.ToDouble(newArr[m]);
                    m++;
                }
            }
            return arr;//返回一个double型的二维数组
        }
    }
}
